package com.ruitu.health_1.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruitu.health_1.R;

/**
 * Created by  on 2018/4/23.
 */
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher {
    private TextView tv_add_num, tv_move_num;
    private TextView etAmount;

    private OnAmountChangeListener onAmountChangeListener;
    private OnTextViewOnClickListener onTextViewOnClickListener;
    private OnAddCheckClickListener onAddCheckClickListener;
    private OnSubtractCheckClickListener onSubtractCheckClickListener;
    private int goods_storage = 1;//商品库存
    private int amount = 1;//购买数量

    public AmountView(Context context) {
        super(context);
    }

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.number_add_sub_view, this);
        tv_add_num = findViewById(R.id.tv_add_num);
        tv_move_num = findViewById(R.id.tv_move_num);
        etAmount = findViewById(R.id.etAmount);
        tv_add_num.setOnClickListener(this);
        tv_move_num.setOnClickListener(this);
        etAmount.setOnClickListener(this);
//        etAmount.addTextChangedListener(this);

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnwidth = typedArray.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT);
        int tvWidth = typedArray.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 60);
        int tvTextSize = typedArray.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0);
        int btnTextSize = typedArray.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0);
        typedArray.recycle();
        LayoutParams btnParams = new LayoutParams(btnwidth, LayoutParams.MATCH_PARENT);
        tv_add_num.setLayoutParams(btnParams);
        tv_move_num.setLayoutParams(btnParams);
        if (btnTextSize != 0) {
            tv_add_num.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
            tv_move_num.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
        }

        LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT);
        etAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            etAmount.setTextSize(tvTextSize);
        }
    }

    public AmountView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.onAmountChangeListener = onAmountChangeListener;
    }

    public void setOnTextViewOnClickListener(OnTextViewOnClickListener onTextViewOnClickListener) {
        this.onTextViewOnClickListener = onTextViewOnClickListener;
    }

    public void setOnAddCheckClickListener(OnAddCheckClickListener onAddCheckClickListener) {
        this.onAddCheckClickListener = onAddCheckClickListener;
    }

    public void setOnSubtractCheckClickListener(OnSubtractCheckClickListener onSubtractCheckClickListener) {
        this.onSubtractCheckClickListener = onSubtractCheckClickListener;
    }

    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
    }

    public void setAmounts(int amounts) {
        this.amount = amounts;
        setAmountEnalbe();
        etAmount.setText(amount + "");
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        setAmountEnalbe();
    }


    @Override
    public void afterTextChanged(Editable editable) {
        if (editable.toString().isEmpty()) {
            return;
        }
        amount = Integer.valueOf(editable.toString());
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }
        if (onAmountChangeListener != null) {
            onAmountChangeListener.onAmountChange(this, amount);
        }
    }

    private void setAmountEnalbe() {
        if (amount <= 1) {
            tv_move_num.setEnabled(false);
        } else {
            tv_move_num.setEnabled(true);
        }

        if (amount >= 99) {
            tv_add_num.setEnabled(false);
        } else {
            tv_add_num.setEnabled(true);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_add_num) {
            if (amount < goods_storage) {
                amount++;
//                etAmount.setText(amount +"");
                setAmounts(amount);
            }
//            onAddCheckClickListener.onAddCheck();
        }
        if (view.getId() == R.id.tv_move_num) {
            if (amount > 1) {
                amount--;
//                etAmount.setText(amount +"");
                setAmounts(amount);
            }
//            onSubtractCheckClickListener.onSubtractCheck();
        }
        etAmount.clearFocus();//清除焦点

        if (onAmountChangeListener != null) {
            onAmountChangeListener.onAmountChange(this, amount);
        }
        if (view.getId() == R.id.etAmount) {
            if (onTextViewOnClickListener != null) {
                onTextViewOnClickListener.onTextViewClick(view);
            }
        }
    }

    //设置字体大小
    public void setTextSize(int btnSize, int numSize) {
        tv_add_num.setTextSize(btnSize);
        tv_move_num.setTextSize(btnSize);
        etAmount.setTextSize(numSize);
    }

    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount);
    }

    public interface OnTextViewOnClickListener {
        void onTextViewClick(View view);
    }

    public interface OnAddCheckClickListener {
        void onAddCheck();
    }

    public interface OnSubtractCheckClickListener {
        void onSubtractCheck();
    }
}
